package cn.rentaotao.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.WebJarsResourceResolver;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import javax.servlet.ServletRegistration;

/**
 * 用来代替 springMvc.xml
 *
 * @author rtt
 * @date 2022/5/11 09:34
 */
@EnableWebMvc
// @EnableAspectJAutoProxy
@ComponentScan(basePackages = "cn.rentaotao.spring")
public class WebConfig implements WebMvcConfigurer {

    /**
     * 创建视图解析器
     * 搭配 @Controller 的返回值使用
     *
     * @return 视图解析器
     */
    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // 设置视图路径
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setExposeContextBeansAsAttributes(true);

        return viewResolver;
    }

    // @Override
    // public void addViewControllers(ViewControllerRegistry registry) {
    //     // 配置根路径展示页面
    //     registry.addViewController("/").setViewName("forward:index.html");
    // }

    /**
     * 在 StandardServletMultipartResolver 无法直接配置文件限制
     * 可以在 {@link DispatcherServletInitializer#customizeRegistration(ServletRegistration.Dynamic)}
     *
     * @return 多媒体解析器
     */
    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

    // @Bean
    // public MultipartResolver multipartResolver() throws IOException {
    //     CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    //     resolver.setUploadTempDir(new FileSystemResource("/Users/rentaotao/A_study/code/rtt-spring/resources"));
    //     resolver.setMaxUploadSize(2097152);
    //     resolver.setMaxInMemorySize(0);
    //
    //     return resolver;
    // }

    /**
     * 配置静态资源的处理
     *
     * @param configurer 配置类
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * @param configurer 内容协商配置类
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // 设置默认的返回媒体类型
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

//    @Bean
//    public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager contentNegotiationManager) {
//        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
//        resolver.setContentNegotiationManager(contentNegotiationManager);
//
//        return resolver;
//    }

    /**
     * 根据 bean 名称查找视图
     *
     * @return
     */
    @Bean
    public ViewResolver beanNameViewResolver() {
        return new BeanNameViewResolver();
    }

    /**
     * @param registry 资源处理器注册表
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加指定路径资源的处理器
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/")
                .resourceChain(false)
                .addResolver(new WebJarsResourceResolver())
                .addResolver(new PathResourceResolver());

        // registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        // registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        // registry.addResourceHandler("/img/**").addResourceLocations("/img/");
    }
}
